<template>
	<view class="container">
		<view class="user-section">
			<view class="bg"></view>
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="user.avatar || '/static/missing-face.png'"></image>
				</view>
				<view class="info-box">
					<view v-if="hasLogin">
						<text class="username">{{user.nickName || '宠兔记账'}}</text>
					</view>
					<view v-else>
						<text class="username" @click="goToLoginPage()">点击登录</text>
					</view>
				</view>
			</view>
		</view>
		<view class="tj-sction">
			<view class="tj-item">
				<text class="num">{{usageDays || 0}}</text>
				<text>使用天数</text>
			</view>

		</view>
		<view class="menu-container">
			<view class="menu-list">
				<u-cell-group>
					<u-cell-item @click="categorySet()" title="收支类型">
					</u-cell-item>
					<u-cell-item v-if="hasLogin" @click="logout()" title="注销登录">
					</u-cell-item>
				</u-cell-group>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		computed: {
			user() {
				return this.$store.getters.user
			},
			usageDays() {
				let days = 0;
				if (this.$store.getters.user != "") {
					// 当前时间
					let date = new Date();
					let currentTime = date.getFullYear() + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
						.getMonth() + 1) + (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
					// 创建时间
					date = new Date(this.$store.getters.user.createTime)
					let createTime = date.getFullYear() + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
						.getMonth() + 1) + (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
					// 已使用天数
					days = currentTime - createTime;
				}
				return days;
			}
		},
		methods: {
			// #ifdef MP
			getCode,
			// #endif
			share() {
				console.log('非小程序分享')
			},
			// 选择账单回调
			billConfirm(e) {
				console.log(e);
			},
			message() {},
			// 退出登陆
			logout() {
				this.$api({
					method: 'post',
					url: '/logout',
					data: {}
				}).then(res => {
					if (res.code == 0) {
						this.$u.toast(res.msg);
						// 清空缓存
						this.$store.commit('token', "")
						this.$store.commit('book', "")
						this.$store.commit('user', {})
						uni.clearStorageSync()
						// 退回到登陆页
						uni.navigateTo({
							url: "/pages/login/login",
							fail(err) {
								console.log(err)
							}
						})
					}
				}).catch(err => {
					this.$u.toast("网络连接失败");
				});

			},
			categorySet() {
				uni.navigateTo({
					url: "/pages/setting/category"
				})
			},

		},

	}
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		height: 100vh;
	}

	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	.user-section {
		padding: 100upx 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background-color: $uni-theme-color;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: 30rpx;
			color: #fff;
			margin-left: 20rpx;

			&.qq_login_btn {
				background-color: $uni-theme-color;
			}
		}
	}

	.menu-container {
		background-color: #f5f5f5;
		position: relative;
		padding-bottom: 20rpx;
	}

	.tj-sction {
		@extend %section;

		background-color: #fff;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 140upx;
			font-size: $font-sm;
			color: $uni-text-color-grey;
		}

		.num {
			font-size: 40rpx;
			color: $uni-text-color;
			margin-bottom: 8upx;
		}
	}

	.menu-list {
		margin-top: 30upx;
		background: #fff;
		border-radius: 10upx;

		switch {
			transform: scale(0.7, 0.7)
		}
	}

	.list-item {
		width: 100%;
		padding: 0rpx;
		background-color: #FFFFFF !important;
		border: initial;
		text-align: initial;
	}

	.list-item::after {
		border: initial;
	}

	.select_bill {
		flex: 1;
		margin-right: 10px;
		text-align: right;
		color: #FFFFFF;
	}
</style>